import Vue from 'vue'

import 'normalize.css/normalize.css' // A modern alternative to CSS resets

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
// import locale from 'element-ui/lib/locale/lang/en' // lang i18n

import '@/styles/index.scss' // global css

import App from './App'
import store from './store'
import router from './router'

import '@/icons' // icon
import '@/permission' // permission control

/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online ! ! !
 */
// if (process.env.NODE_ENV === 'production') {
//   const { mockXHR } = require('../mock')
//   mockXHR()
// }

// set ElementUI lang to EN
Vue.use(ElementUI)
// 如果想要中文版 element-ui，按如下方式声明
// Vue.use(ElementUI)

Vue.config.productionTip = false

// 封装自定义指令 用来控制操作权
Vue.directive('permission', {
  inserted(el, binding) {
    // 等待用户信息加载完成
    if (!store.state.user.userInfo) {
      // 如果用户信息未加载，先隐藏元素
      el.style.display = 'none'
      // 监听用户信息变化
      const unwatch = store.watch(
        state => state.user.userInfo,
        (newVal) => {
          if (newVal) {
            const points = newVal.roles?.points || []
            if (!points.includes(binding.value)) {
              el.remove()
            } else {
              el.style.display = ''
            }
            unwatch() // 取消监听
          }
        }
      )
    } else {
      const points = store.state.user.userInfo.roles?.points || []
      if (!points.includes(binding.value)) {
        el.remove()
      }
    }
  }
})

new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
